import { AnyAction } from "redux";
import * as actionTypes from './constants'

interface IRecommendItem {
    sort: number,
    sampleStatus: number,
    favoriteCount: number,
    designCover: string,
    designName: string,
    browseCount: string
    designerAvatar: string,
    designerName: string
}
export type IRecommendList = IRecommendItem[]
export interface RecommendState {
    recommendList: IRecommendList;
    enterLoading: boolean;
}
const defaultState: RecommendState = {
    recommendList: [],
    enterLoading: true
};

export const recommendReducer = (state = defaultState, action: AnyAction) => {
    switch(action.type) {
        case actionTypes.CHANGE_RECOMMEND_LIST:
            return {
                ...state,
                recommendList: action.data
            }
        case actionTypes.CHANGE_LOADING:
            return {
                ...state,
                enterLoading: action.data
            }
        case actionTypes.CHANGE_DIANZAN:
            return {
                ...state,
                recommendList: state.recommendList.map(item => {
                    if (item.sort == action.id) {
                        // aixin 未点赞爱心状态
                        if(item.sampleStatus == 0) {
                            item.favoriteCount++;
                            item.sampleStatus++;
                        } else {
                            item.favoriteCount--;
                            item.sampleStatus--
                        }
                    }
                    return item
                })
            }
        default:
            return state     
    }
}